package org.btik.light.mobile.common.handler.adaptor;

import android.view.View;
import android.widget.SeekBar;
import org.btik.light.mobile.common.handler.DevBinder;
import org.btik.light.mobile.meta.mo.DefaultDevMo;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class SpeedHandler implements SeekBar.OnSeekBarChangeListener, DevBinder {

    private static final String SPEED = "speed";
    public static final int MAX_SPEED = 100;
    private final BiConsumer<Integer, View> setSpeedController;

    private final Consumer<View> maxSpeedController;

    /**
     * 速度显示，用于刷新界面
     */
    private BiConsumer<Integer, Boolean> speedShow;

    private DefaultDevMo defaultDevMo;


    public SpeedHandler(BiConsumer<Integer, View> setSpeedCommand, Consumer<View> maxSpeedCommand) {
        this.setSpeedController = setSpeedCommand;
        this.maxSpeedController = maxSpeedCommand;
    }

    private void setSpeedShow(Integer progress) {
        setSpeedShow(progress, true);
    }

    public void setSpeedShow(BiConsumer<Integer, Boolean> speedShow) {
        this.speedShow = speedShow;
    }

    /**
     * @param setSeekBar 是否设置SeekBar上的进度
     */
    private void setSpeedShow(Integer progress, boolean setSeekBar) {
        if (speedShow != null) {
            speedShow.accept(progress, setSeekBar);
        }
        defaultDevMo.setExtendedAttribute(SPEED, progress.toString());
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        setSpeedShow(progress, false);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int speed = getSpeed();
        sendCmd(speed, seekBar);
    }

    public void speedUp(View view) {
        int speed = getSpeed();
        if (speed < 0) {
            return;
        }
        speed += 5;
        speed = Math.min(speed, MAX_SPEED);
        setSpeedShow(speed);
        sendCmd(speed, view);

    }

    public void goPikachu(View view) {
        setSpeedShow(MAX_SPEED);
        maxSpeedController.accept(view);
    }

    public void speedDown(View view) {
        int speed = getSpeed();
        if (speed < 5) {
            return;
        }
        speed -= 5;
        speed = Math.max(1, speed);
        setSpeedShow(speed);
        sendCmd(speed, view);
    }

    private int getSpeed() {
        String attr = defaultDevMo.getAttr(SPEED);
        int speed = 100;
        try {
            speed = Integer.parseInt(attr);
        } catch (Exception e) {
            defaultDevMo.setExtendedAttribute(SPEED, 100);
        }
        return speed;
    }

    /**
     * @param speed 速度取值 [1 ~100],需要先判断
     */
    private void sendCmd(int speed, View view) {
        setSpeedController.accept(speed, view);
    }

    @Override
    public void bind(DefaultDevMo devMo) {
        this.defaultDevMo = devMo;
        if (setSpeedController instanceof DevBinder) {
            ((DevBinder) setSpeedController).bind(devMo);
        }
        if (maxSpeedController instanceof DevBinder) {
            ((DevBinder) maxSpeedController).bind(devMo);
        }
    }
}
